{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查看dicom文件信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pydicom\n",
    "\n",
    "instance_info = pydicom.read_file('/Resources//MRP/1.3.12.2.1107.5.2.32.35036.2015052301481365184810523.dcm')\n",
    "StudyInstanceUID = instance_info.get('ImageType') #instance_info中的标签直接结合get\n",
    "StudyInstanceUID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bmmodule.utils import protocols\n",
    "from bmutils.series import generate_series,SeriesClass\n",
    "from brain_ctp_mrp.brain_mrp.aibrainmrp import IntegrationPipeline\n",
    "\n",
    "dicom_folder = '/deps/Biomind-Test-Data/data/MRP-TEST/dicom'\n",
    "series = generate_series(dicom_folder)  # dict, key:series+time value:dcm_file\n",
    "metadata = generate_metadata_dicom(series)\n",
    "\n",
    "user_classification = {\n",
    "    # 'PWI': '1.3.12.2.1107.5.2.32.35036.2014112113013923128107746.0.0.0.215128128',\n",
    "    # 'DWI': '1.3.12.2.1107.5.2.32.35036.2014042923300893099491061.0.0.0.35128128',\n",
    "    # 'ADC': '1.3.12.2.1107.5.2.32.35036.201505230148037020010505.0.0.0.155128128',\n",
    "    \"DWI\": \"1.3.12.2.1107.5.2.32.35036.2015052223171291121098281.0.0.0\",\n",
    "    \"ADC\": \"1.3.12.2.1107.5.2.32.35036.2015052223171291121298282.0.0.0\",\n",
    "    \"PWI\": \"1.3.12.2.1107.5.2.32.35036.201505222355236939601354.0.0.0\",\n",
    "}\n",
    "\n",
    "uid_translate = uid_mapper(\n",
    "    user_classification.values(),\n",
    "    series.keys(),\n",
    "    series_uids=user_classification.values()\n",
    ")\n",
    "\n",
    "payload = {\n",
    "    'pdicom_folder':  dicom_folder,\n",
    "    'planguage': 'en-us',\n",
    "    'pseries': series,\n",
    "    'pmetadata': metadata,\n",
    "    'pseries_classifier': {k: uid_translate[v] for k, v in user_classification.items()},\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mrp_series_uid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mrp_protocols = IntegrationPipeline(\n",
    "    args={},\n",
    "    mode='production',\n",
    "    tensorrt=tensorrt,\n",
    "    cache={}, \n",
    "    predict_config={}\n",
    ")(payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bmutils.dicom.defs import tag_values,tags\n",
    "from bmutils.series import generate_series_dicom, generate_metadata_dicom\n",
    "series = generate_series_dicom(payload['pdicom_folder'])\n",
    "metadata = generate_metadata_dicom(series)\n",
    "metadata[tags.Modality]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "series_classifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_series = generate_series(CTP_folder)\n",
    "ctp_series_uid = list(all_series.keys())[0]\n",
    "len(all_series[ctp_series_uid])#序列长度32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tags.Modality\n",
    "metadata['Modality']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "tensorrt = \"http://192.168.2.44:7000\"\n",
    "model_manager=None\n",
    "# fix dicom orientation\n",
    "if protocols.dicom_folder in payload:\n",
    "    series_by_orientation = deepcopy(SortSeriesByOrientation()(series))\n",
    "elif protocols.mhd_folder in payload:\n",
    "    series_by_orientation = deepcopy(series)\n",
    "\n",
    "mrp_series_uid = series_classifier.get(SeriesClass.MRP.key)\n",
    "mrp_series = series_by_orientation[mrp_series_uid]\n",
    "\n",
    "sortinstance = [pydicom.read_file(s).get(\"SOPInstanceUID\") for s in mrp_series ]\n",
    "payload.update({\n",
    "    'psorted_series': sortinstance\n",
    "})\n",
    "cache = {}\n",
    "# cache_key = f\"_intermediate\" + hashlib.md5(pickle.dumps(mrp_series_uid)).hexdigest()\n",
    "# if cache_data.get('cache_key') == cache_key:\n",
    "#     cache = cache_data.get('data')\n",
    "\n",
    "# get cta predict config\n",
    "predict_config = {}\n",
    "pconfig = payload.get(protocols.config)\n",
    "if pconfig:\n",
    "    predict_config = pconfig.get('mrp') or pconfig\n",
    "\n",
    "mrp_protocols = IntegrationPipeline(\n",
    "    args={},\n",
    "    mode='production',\n",
    "    tensorrt=tensorrt,\n",
    "    cache={}, \n",
    "    predict_config={}\n",
    ")(payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from bmutils.utils import str2npy\n",
    "for x in mrp_protocols['pprediction'][mrp_series_uid]:#检测序列长度\n",
    "    for k,v in x.items():\n",
    "        if k==\"mask\":\n",
    "            print(str2npy(v).shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bmutils.series import SeriesClass\n",
    "SeriesClass.DWI.key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bmutils.dicom.defs import tag_values,tags\n",
    "metadata[tags.Modality]\n",
    "#metadata[tag.Modality]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "series_classifier.get(SeriesClass.MRP.key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mrp_protocols['pangiography']['1.3.12.2.1107.5.2.32.35036.201505230148037020010505.0.0.0.155128128.vol<1,50>']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "with open(\"/Resources/model_output.pkl\", \"wb\") as fn:\n",
    "    pickle.dump(mrp_protocols, fn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "with open(\"/Resources/output_mrp.json\", \"rb\") as fn:\n",
    "    result = json.load(fn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#result[\"pprediction\"][\"1.3.12.2.1107.5.2.32.35036.201505230148037020010505.0.0.0.155128128.vol<50,50>\"]\n",
    "from bmutils.utils import npy2str, str2npy\n",
    "for x,t in enumerate(result[\"pprediction\"][\"1.3.12.2.1107.5.2.32.35036.201505230148037020010505.0.0.0.155128128.vol<50,50>\"]):\n",
    "    print(str2npy(t[\"mask\"]).shape)\n",
    "    #print(t[\"segment_id'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result['pprediction'][\"1.3.12.2.1107.5.2.32.35036.201505230148037020010505.0.0.0.155128128.vol<50,50>\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list(all_series.values())[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "instance_info = dicom.read_file(list(all_series.values())[0][1])\n",
    "#instance_info.data_element('Study Instance UID')\n",
    "#data_sitk = sitk.ReadImage(all_series[list(all_series.keys())[0]][0])\n",
    "#img_array = sitk.GetArrayFromImage(data_sitk)\n",
    "#instance_info.dir()#打印所有属性\n",
    "StudyInstanceUID = instance_info.get('StudyInstanceUID')\n",
    "SeriesInstanceUID = instance_info.get('SeriesInstanceUID')\n",
    "SeriesInstanceUID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "StudyInstanceUID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name_file_list = os.listdir(MRP_folder)\n",
    "path_file_first = os.path.join(MRP_folder, name_file_list[0])\n",
    "data = dicom.read_file(path_file_first)\n",
    "data_sitk = sitk.ReadImage(path_file_first)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_series[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "instance_info = dicom.read_file(all_series[0])\n",
    "data_sitk = sitk.ReadImage(series[0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
